import QtQuick 2.0
import GpioDataitem 1.0
import SerialDataitem 1.0

Rectangle {
    id:standarWait
    color: "#FFFFFF"


    signal sigCancelClick();
    signal sigSuccessClick();


    property int currentAllPpm: 0
    property int currentPpm: 0
    property int currentMaxPpm: 0
    property int currentNums: 0



    MouseArea{
        anchors.fill: parent
        onPressed: {
        }
        onReleased: {
        }
        onClicked: {
        }
    }

    onVisibleChanged: {

        if(visible){

            currentAllPpm= 0
            currentPpm= 0
            currentMaxPpm = 0;
            currentNums = 0;
            if(gpioDataitem.getD1() == 0){
                gpioDataitem.openD1();
            }
            if(gpioDataitem.getD2() == 1){
                gpioDataitem.closeD2();
            }
            if(gpioDataitem.getD3() == 1){
                gpioDataitem.closeD3();
            }


            gpioDataitem.openD4();

           // serialDataitem.readCoPotency();

            exhalePercent.percent = 0;
            exhaleTimer.start();
            handCurrentDate();
        }else{
            exhalePercent.percent = 0;
            exhaleTimer.stop();
        }
    }



    function handCurrentDate(){
        var currentDate = new Date();
        var date_tring = currentDate.toLocaleDateString();
    //    console.log("local = ",date_tring,currentDate.getFullYear(),currentDate.getDay())
        var curyear = parseInt(currentDate.getFullYear().toString());
        var curmonth = parseInt((currentDate.getMonth()+1).toString())
        var curday = parseInt(currentDate.getDate().toString())


        dateText.text = curyear+qsTr("年")+curmonth+qsTr("月")+curday+qsTr("日");
    }
    //顶部背景
    Rectangle{
        id:topBackground

        anchors.left: parent.left
        anchors.top: parent.top
        width: parent.width
        height: 80
        color: "#E8C06B"
        Text {
            id: topMacheneName
            width: parent.width
            height: parent.height
            color: "#FFFFFF"
            font.bold: true
            font.italic: true
            font.pixelSize: 30
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
            text: qsTr("呼气分析仪")
        }


        Text {
            id: dateText
            anchors.left:  parent.left
            anchors.leftMargin: 10
            anchors.top: parent.top
            height: parent.height
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignLeft
            width: dateText.contentWidth
            color: "#FFFFFF"
            font.pixelSize: 16
            text: qsTr("")
        }
        Timer {
            id: dateTimer
            interval: 60000;
            running: standarWait.visible;
            repeat: true
            onTriggered:{
                handCurrentDate();
            }
        }

    }
    //中间背景
    Rectangle{
        id:midBackground
        anchors.left: parent.left
        anchors.top: topBackground.bottom
        anchors.bottom: bottomBackground.top
        width: parent.width
        color: "#FFFFFF"
        Rectangle{
            id:midInforBackground
            width: 400
            height: 300
            anchors.right:  parent.right
            anchors.verticalCenter:parent.verticalCenter
            anchors.rightMargin: 60
            radius: 10
            color: "#F2F2F2"
            Text {
                id: coTipText
                anchors.left:  parent.left
                anchors.leftMargin: 20
                anchors.top: parent.top
                anchors.topMargin: 20
                height: 60
                verticalAlignment: Text.AlignVCenter
                horizontalAlignment: Text.AlignLeft
                width: coTipText.contentWidth
                color: "#E8C06B"
                font.pixelSize: 35
                font.bold: true
                text: qsTr("标准气等待测量:")
            }

            ExhalePercent{
                id:exhalePercent
                anchors.left:    parent.left
                anchors.leftMargin: 30
                anchors.bottom:  parent.bottom
                anchors.bottomMargin: 20
                width: 200
                height: 200
            }

            Rectangle{
                id:cancelBtn

                anchors.right:  parent.right
                anchors.rightMargin: 10
                anchors.bottom: parent.bottom
                anchors.bottomMargin: 15
                width: 80
                height: 30
                radius: 20
                color:( cancelBtn.cancelBtnPressed ? "#5C38373A":"#FF38373A")

                property bool cancelBtnPressed: false
                Text {
                    width: parent.width
                    height: parent.height
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignHCenter
                    font.pixelSize: 15
                    color: "#FDE1C4"
                    text: qsTr("取消")
                }

                MouseArea{
                    anchors.fill: parent

                    onPressed: {
                        cancelBtn.cancelBtnPressed = true
                    }
                    onReleased: {
                        cancelBtn.cancelBtnPressed = false
                    }
                    onClicked: {
                        if(gpioDataitem.getD1() == 1){
                            gpioDataitem.closeD1();
                        }
                        if(gpioDataitem.getD2() == 1){
                            gpioDataitem.closeD2();
                        }
                        if(gpioDataitem.getD3() == 1){
                            gpioDataitem.closeD3();
                        }
                        if(gpioDataitem.getD4() == 1){
                            gpioDataitem.closeD4();
                        }

                        standarWait.sigCancelClick();
                    }
                }

            }


        }


        ClockDial{
            id:clockDial
            anchors.left:  parent.left
            anchors.leftMargin: 30
            anchors.verticalCenter:parent.verticalCenter
            //  visible: exhaleWait.visible

        }
    }
    //底部背景
    Rectangle{
        id:bottomBackground
        anchors.left: parent.left
        anchors.bottom: parent.bottom
        width: parent.width
        height: 80
        color: "#E8C06B"


        Text {
            id: produceText
            anchors.left:  parent.left
            anchors.leftMargin: 35
            anchors.top: parent.top
            height: parent.height
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignLeft
            width: produceText.contentWidth
            color: "#FFFFFF"
            font.pixelSize: 18
            text: qsTr("产品名称")
        }
        Text {
            id: companyText
            anchors.right:   parent.right
            anchors.rightMargin: 35
            anchors.top: parent.top
            height: parent.height
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignRight
            width: companyText.contentWidth
            color: "#FFFFFF"
            font.pixelSize: 22
            text: qsTr("公司名称")
        }
    }


    Timer{
        id:exhaleTimer
        repeat: false
        running: false
        interval: 1000


        onTriggered: {
            exhaleTimer.stop()


            exhalePercent.percent += 10;

            if(currentNums <= 4){
            //    serialDataitem.readCoPotency();

            }else{

                if(currentNums == 5){
                    if(gpioDataitem.getD1() == 1){
                        gpioDataitem.closeD1();
                    }
                    if(gpioDataitem.getD2() == 1){
                        gpioDataitem.closeD2();
                    }

                    if(gpioDataitem.getD4() == 1){
                        gpioDataitem.closeD4();
                    }

                    if(gpioDataitem.getD3() == 0){
                        gpioDataitem.openD3();
                    }
                }else{

                    if(currentNums >= 8){
                        if(gpioDataitem.getD3() == 1){
                            gpioDataitem.closeD3();
                        }
                    }
                }
            }
            currentNums++;

            if(exhalePercent.percent <= 100){

                exhaleTimer.start();
            }else{
                if(gpioDataitem.getD1() == 1){
                    gpioDataitem.closeD1();
                }
                if(gpioDataitem.getD2() == 1){
                    gpioDataitem.closeD2();
                }
                if(gpioDataitem.getD3() == 1){
                    gpioDataitem.closeD3();
                }
                if(gpioDataitem.getD4() == 1){
                    gpioDataitem.closeD4();
                }
                currentPpm = currentAllPpm / 5
                standarWait.sigSuccessClick();
            }
        }

    }



    GpioDataitem{
        id:gpioDataitem
    }

    SerialDataitem{
        id:serialDataitem

        onSigSendPotency: {
            currentAllPpm += potency;

            if(currentMaxPpm < potency){
                currentMaxPpm = potency
            }

        }
    }


}
